
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/matrices/sparse.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:27:05 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Sparse Matrices &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="sparse.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Sparse Tools" href="sparsetools.html" />
    <link rel="prev" title="Dense Matrices" href="dense.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="sparsetools.html" title="Sparse Tools"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="dense.html" title="Dense Matrices"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Matrices</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Sparse Matrices</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="module-sympy.matrices.sparse">
<span id="sparse-matrices"></span><h1>Sparse Matrices<a class="headerlink" href="#module-sympy.matrices.sparse" title="Permalink to this headline">¶</a></h1>
<section id="sparsematrix-class-reference">
<h2>SparseMatrix Class Reference<a class="headerlink" href="#sparsematrix-class-reference" title="Permalink to this headline">¶</a></h2>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.matrices.sparse.</span></span><span class="sig-name descname"><span class="pre">SparseMatrix</span></span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L20-L455"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix" title="Permalink to this definition">¶</a></dt>
<dd><p>A sparse matrix (a matrix with a large number of zero elements).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">SparseMatrix</span><span class="p">,</span> <span class="n">ones</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Matrix([</span>
<span class="go">[0, 1],</span>
<span class="go">[2, 3]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">{(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">):</span> <span class="mi">2</span><span class="p">})</span>
<span class="go">Matrix([</span>
<span class="go">[0, 0],</span>
<span class="go">[0, 2]])</span>
</pre></div>
</div>
<p>A SparseMatrix can be instantiated from a ragged list of lists:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">]])</span>
<span class="go">Matrix([</span>
<span class="go">[1, 2, 3],</span>
<span class="go">[1, 2, 0],</span>
<span class="go">[1, 0, 0]])</span>
</pre></div>
</div>
<p>For safety, one may include the expected size and then an error
will be raised if the indices of any element are out of range or
(for a flat list) if the total number of elements does not match
the expected shape:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">List length (2) != rows*columns (4)</span>
</pre></div>
</div>
<p>Here, an error is not raised because the list is not flat and no
element is out of range:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]])</span>
<span class="go">Matrix([</span>
<span class="go">[1, 2],</span>
<span class="go">[0, 0]])</span>
</pre></div>
</div>
<p>But adding another element to the first (and only) row will cause
an error to be raised:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]])</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">The location (0, 2) is out of designated range: (1, 1)</span>
</pre></div>
</div>
<p>To autosize the matrix, pass None for rows:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]])</span>
<span class="go">Matrix([[1, 2, 3]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="p">{(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">):</span> <span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">):</span> <span class="mi">3</span><span class="p">})</span>
<span class="go">Matrix([</span>
<span class="go">[0, 0, 0, 0],</span>
<span class="go">[0, 1, 0, 0],</span>
<span class="go">[0, 0, 0, 0],</span>
<span class="go">[0, 0, 0, 3]])</span>
</pre></div>
</div>
<p>Values that are themselves a Matrix are automatically expanded:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="p">{(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">):</span> <span class="n">ones</span><span class="p">(</span><span class="mi">2</span><span class="p">)})</span>
<span class="go">Matrix([</span>
<span class="go">[0, 0, 0, 0],</span>
<span class="go">[0, 1, 1, 0],</span>
<span class="go">[0, 1, 1, 0],</span>
<span class="go">[0, 0, 0, 0]])</span>
</pre></div>
</div>
<p>A ValueError is raised if the expanding matrix tries to overwrite
a different element already present:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SparseMatrix</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="p">{(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span> <span class="n">ones</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">):</span> <span class="mi">2</span><span class="p">})</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">collision at (1, 1)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="dense.html#sympy.matrices.dense.DenseMatrix" title="sympy.matrices.dense.DenseMatrix"><code class="xref py py-obj docutils literal notranslate"><span class="pre">DenseMatrix</span></code></a>, <a class="reference internal" href="#sympy.matrices.sparse.MutableSparseMatrix" title="sympy.matrices.sparse.MutableSparseMatrix"><code class="xref py py-obj docutils literal notranslate"><span class="pre">MutableSparseMatrix</span></code></a>, <a class="reference internal" href="#sympy.matrices.immutable.ImmutableSparseMatrix" title="sympy.matrices.immutable.ImmutableSparseMatrix"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ImmutableSparseMatrix</span></code></a></p>
</div>
<dl class="py property">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.CL">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">CL</span></span><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.CL" title="Permalink to this definition">¶</a></dt>
<dd><p>Alternate faster representation</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.LDLdecomposition">
<span class="sig-name descname"><span class="pre">LDLdecomposition</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">hermitian</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L441-L442"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.LDLdecomposition" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the LDL Decomposition (matrices <code class="docutils literal notranslate"><span class="pre">L</span></code> and <code class="docutils literal notranslate"><span class="pre">D</span></code>) of matrix
<code class="docutils literal notranslate"><span class="pre">A</span></code>, such that <code class="docutils literal notranslate"><span class="pre">L</span> <span class="pre">*</span> <span class="pre">D</span> <span class="pre">*</span> <span class="pre">L.T</span> <span class="pre">==</span> <span class="pre">A</span></code>. <code class="docutils literal notranslate"><span class="pre">A</span></code> must be a square,
symmetric, positive-definite and non-singular.</p>
<p>This method eliminates the use of square root and ensures that all
the diagonal entries of L are 1.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">SparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">(((</span><span class="mi">25</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">),</span> <span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">11</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="p">,</span> <span class="n">D</span> <span class="o">=</span> <span class="n">A</span><span class="o">.</span><span class="n">LDLdecomposition</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
<span class="go">Matrix([</span>
<span class="go">[   1,   0, 0],</span>
<span class="go">[ 3/5,   1, 0],</span>
<span class="go">[-1/5, 1/3, 1]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">D</span>
<span class="go">Matrix([</span>
<span class="go">[25, 0, 0],</span>
<span class="go">[ 0, 9, 0],</span>
<span class="go">[ 0, 0, 9]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">*</span> <span class="n">D</span> <span class="o">*</span> <span class="n">L</span><span class="o">.</span><span class="n">T</span> <span class="o">==</span> <span class="n">A</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.RL">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">RL</span></span><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.RL" title="Permalink to this definition">¶</a></dt>
<dd><p>Alternate faster representation</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.applyfunc">
<span class="sig-name descname"><span class="pre">applyfunc</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L251-L281"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.applyfunc" title="Permalink to this definition">¶</a></dt>
<dd><p>Apply a function to each element of the matrix.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">SparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">:</span> <span class="n">i</span><span class="o">*</span><span class="mi">2</span><span class="o">+</span><span class="n">j</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span>
<span class="go">Matrix([</span>
<span class="go">[0, 1],</span>
<span class="go">[2, 3]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">applyfunc</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="mi">2</span><span class="o">*</span><span class="n">i</span><span class="p">)</span>
<span class="go">Matrix([</span>
<span class="go">[0, 2],</span>
<span class="go">[4, 6]])</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.as_immutable">
<span class="sig-name descname"><span class="pre">as_immutable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L283-L286"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.as_immutable" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an Immutable version of this Matrix.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.as_mutable">
<span class="sig-name descname"><span class="pre">as_mutable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L288-L303"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.as_mutable" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a mutable version of this matrix.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">ImmutableMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span> <span class="n">ImmutableMatrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Y</span> <span class="o">=</span> <span class="n">X</span><span class="o">.</span><span class="n">as_mutable</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Y</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">5</span> <span class="c1"># Can set values in Y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Y</span>
<span class="go">Matrix([</span>
<span class="go">[1, 2],</span>
<span class="go">[3, 5]])</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.cholesky">
<span class="sig-name descname"><span class="pre">cholesky</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">hermitian</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L438-L439"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.cholesky" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the Cholesky decomposition L of a matrix A
such that L * L.T = A</p>
<p>A must be a square, symmetric, positive-definite
and non-singular matrix</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">SparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">(((</span><span class="mi">25</span><span class="p">,</span><span class="mi">15</span><span class="p">,</span><span class="o">-</span><span class="mi">5</span><span class="p">),(</span><span class="mi">15</span><span class="p">,</span><span class="mi">18</span><span class="p">,</span><span class="mi">0</span><span class="p">),(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">11</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">cholesky</span><span class="p">()</span>
<span class="go">Matrix([</span>
<span class="go">[ 5, 0, 0],</span>
<span class="go">[ 3, 3, 0],</span>
<span class="go">[-1, 1, 3]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">cholesky</span><span class="p">()</span> <span class="o">*</span> <span class="n">A</span><span class="o">.</span><span class="n">cholesky</span><span class="p">()</span><span class="o">.</span><span class="n">T</span> <span class="o">==</span> <span class="n">A</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The matrix can have complex entries:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">I</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">(((</span><span class="mi">9</span><span class="p">,</span> <span class="mi">3</span><span class="o">*</span><span class="n">I</span><span class="p">),</span> <span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="o">*</span><span class="n">I</span><span class="p">,</span> <span class="mi">5</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">cholesky</span><span class="p">()</span>
<span class="go">Matrix([</span>
<span class="go">[ 3, 0],</span>
<span class="go">[-I, 2]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">cholesky</span><span class="p">()</span> <span class="o">*</span> <span class="n">A</span><span class="o">.</span><span class="n">cholesky</span><span class="p">()</span><span class="o">.</span><span class="n">H</span>
<span class="go">Matrix([</span>
<span class="go">[   9, 3*I],</span>
<span class="go">[-3*I,   5]])</span>
</pre></div>
</div>
<p>Non-hermitian Cholesky-type decomposition may be useful when the
matrix is not positive-definite.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="n">A</span><span class="o">.</span><span class="n">cholesky</span><span class="p">(</span><span class="n">hermitian</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
<span class="go">Matrix([</span>
<span class="go">[1,         0],</span>
<span class="go">[2, sqrt(3)*I]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">*</span><span class="n">L</span><span class="o">.</span><span class="n">T</span> <span class="o">==</span> <span class="n">A</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.matrices.sparse.SparseMatrix.LDLdecomposition" title="sympy.matrices.sparse.SparseMatrix.LDLdecomposition"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.matrices.sparse.SparseMatrix.LDLdecomposition</span></code></a>, <a class="reference internal" href="matrices.html#sympy.matrices.matrices.MatrixBase.LUdecomposition" title="sympy.matrices.matrices.MatrixBase.LUdecomposition"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.matrices.matrices.MatrixBase.LUdecomposition</span></code></a>, <a class="reference internal" href="matrices.html#sympy.matrices.matrices.MatrixBase.QRdecomposition" title="sympy.matrices.matrices.MatrixBase.QRdecomposition"><code class="xref py py-obj docutils literal notranslate"><span class="pre">QRdecomposition</span></code></a></p>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.col_list">
<span class="sig-name descname"><span class="pre">col_list</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L305-L325"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.col_list" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a column-sorted list of non-zero elements of the matrix.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">SparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">=</span><span class="n">SparseMatrix</span><span class="p">(((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">Matrix([</span>
<span class="go">[1, 2],</span>
<span class="go">[3, 4]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">CL</span>
<span class="go">[(0, 0, 1), (1, 0, 3), (0, 1, 2), (1, 1, 4)]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.matrices.sparse.SparseMatrix.row_list" title="sympy.matrices.sparse.SparseMatrix.row_list"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.matrices.sparse.SparseMatrix.row_list</span></code></a></p>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.liupc">
<span class="sig-name descname"><span class="pre">liupc</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L432-L433"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.liupc" title="Permalink to this definition">¶</a></dt>
<dd><p>Liu’s algorithm, for pre-determination of the Elimination Tree of
the given matrix, used in row-based symbolic Cholesky factorization.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">SparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">([</span>
<span class="gp">... </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
<span class="gp">... </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
<span class="gp">... </span><span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span>
<span class="gp">... </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">0</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span><span class="o">.</span><span class="n">liupc</span><span class="p">()</span>
<span class="go">([[0], [], [0], [1, 2]], [4, 3, 4, 4])</span>
</pre></div>
</div>
<p class="rubric">References</p>
<p>Symbolic Sparse Cholesky Factorization using Elimination Trees,
Jeroen Van Grondelle (1999)
<a class="reference external" href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.7582">http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.7582</a></p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.nnz">
<span class="sig-name descname"><span class="pre">nnz</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L327-L329"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.nnz" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the number of non-zero elements in Matrix.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.row_list">
<span class="sig-name descname"><span class="pre">row_list</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L331-L352"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.row_list" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a row-sorted list of non-zero elements of the matrix.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">SparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">(((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">Matrix([</span>
<span class="go">[1, 2],</span>
<span class="go">[3, 4]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">RL</span>
<span class="go">[(0, 0, 1), (0, 1, 2), (1, 0, 3), (1, 1, 4)]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.matrices.sparse.SparseMatrix.col_list" title="sympy.matrices.sparse.SparseMatrix.col_list"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.matrices.sparse.SparseMatrix.col_list</span></code></a></p>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.row_structure_symbolic_cholesky">
<span class="sig-name descname"><span class="pre">row_structure_symbolic_cholesky</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L435-L436"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.row_structure_symbolic_cholesky" title="Permalink to this definition">¶</a></dt>
<dd><p>Symbolic cholesky factorization, for pre-determination of the
non-zero structure of the Cholesky factororization.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">SparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">([</span>
<span class="gp">... </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
<span class="gp">... </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
<span class="gp">... </span><span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span>
<span class="gp">... </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">0</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span><span class="o">.</span><span class="n">row_structure_symbolic_cholesky</span><span class="p">()</span>
<span class="go">[[0], [], [0], [1, 2]]</span>
</pre></div>
</div>
<p class="rubric">References</p>
<p>Symbolic Sparse Cholesky Factorization using Elimination Trees,
Jeroen Van Grondelle (1999)
<a class="reference external" href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.7582">http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.7582</a></p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.scalar_multiply">
<span class="sig-name descname"><span class="pre">scalar_multiply</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">scalar</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L354-L356"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.scalar_multiply" title="Permalink to this definition">¶</a></dt>
<dd><p>Scalar element-wise multiplication</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.solve">
<span class="sig-name descname"><span class="pre">solve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">rhs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'LDL'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L415-L427"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.solve" title="Permalink to this definition">¶</a></dt>
<dd><p>Return solution to self*soln = rhs using given inversion method.</p>
<p>For a list of possible inversion methods, see the .inv() docstring.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.matrices.sparse.SparseMatrix.solve_least_squares">
<span class="sig-name descname"><span class="pre">solve_least_squares</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">rhs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'LDL'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L358-L413"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.SparseMatrix.solve_least_squares" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the least-square fit to the data.</p>
<p>By default the cholesky_solve routine is used (method=’CH’); other
methods of matrix inversion can be used. To find out which are
available, see the docstring of the .inv() method.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">SparseMatrix</span><span class="p">,</span> <span class="n">Matrix</span><span class="p">,</span> <span class="n">ones</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">B</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span> <span class="o">=</span> <span class="n">SparseMatrix</span><span class="p">(</span><span class="n">A</span><span class="o">.</span><span class="n">row_join</span><span class="p">(</span><span class="n">B</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span>
<span class="go">Matrix([</span>
<span class="go">[1, 2],</span>
<span class="go">[2, 3],</span>
<span class="go">[3, 4]])</span>
</pre></div>
</div>
<p>If each line of S represent coefficients of Ax + By
and x and y are [2, 3] then S*xy is:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">S</span><span class="o">*</span><span class="n">Matrix</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]);</span> <span class="n">r</span>
<span class="go">Matrix([</span>
<span class="go">[ 8],</span>
<span class="go">[13],</span>
<span class="go">[18]])</span>
</pre></div>
</div>
<p>But let’s add 1 to the middle value and then solve for the
least-squares value of xy:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">xy</span> <span class="o">=</span> <span class="n">S</span><span class="o">.</span><span class="n">solve_least_squares</span><span class="p">(</span><span class="n">Matrix</span><span class="p">([</span><span class="mi">8</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">18</span><span class="p">]));</span> <span class="n">xy</span>
<span class="go">Matrix([</span>
<span class="go">[ 5/3],</span>
<span class="go">[10/3]])</span>
</pre></div>
</div>
<p>The error is given by S*xy - r:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">S</span><span class="o">*</span><span class="n">xy</span> <span class="o">-</span> <span class="n">r</span>
<span class="go">Matrix([</span>
<span class="go">[1/3],</span>
<span class="go">[1/3],</span>
<span class="go">[1/3]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span><span class="o">.</span><span class="n">norm</span><span class="p">()</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">0.58</span>
</pre></div>
</div>
<p>If a different xy is used, the norm will be higher:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">xy</span> <span class="o">+=</span> <span class="n">ones</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">S</span><span class="o">*</span><span class="n">xy</span> <span class="o">-</span> <span class="n">r</span><span class="p">)</span><span class="o">.</span><span class="n">norm</span><span class="p">()</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">1.5</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.matrices.sparse.MutableSparseMatrix">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.matrices.sparse.</span></span><span class="sig-name descname"><span class="pre">MutableSparseMatrix</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/sparse.py#L458-L466"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.sparse.MutableSparseMatrix" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</section>
<section id="immutablesparsematrix-class-reference">
<h2>ImmutableSparseMatrix Class Reference<a class="headerlink" href="#immutablesparsematrix-class-reference" title="Permalink to this headline">¶</a></h2>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.matrices.immutable.ImmutableSparseMatrix">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.matrices.immutable.</span></span><span class="sig-name descname"><span class="pre">ImmutableSparseMatrix</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/immutable.py#L139-L180"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.immutable.ImmutableSparseMatrix" title="Permalink to this definition">¶</a></dt>
<dd><p>Create an immutable version of a sparse matrix.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">eye</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices.immutable</span> <span class="kn">import</span> <span class="n">ImmutableSparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ImmutableSparseMatrix</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="p">{})</span>
<span class="go">Matrix([[0]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ImmutableSparseMatrix</span><span class="p">(</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="go">Matrix([</span>
<span class="go">[1, 0, 0],</span>
<span class="go">[0, 1, 0],</span>
<span class="go">[0, 0, 1]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">42</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">Cannot set values of ImmutableSparseMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(3, 3)</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Sparse Matrices</a><ul>
<li><a class="reference internal" href="#sparsematrix-class-reference">SparseMatrix Class Reference</a></li>
<li><a class="reference internal" href="#immutablesparsematrix-class-reference">ImmutableSparseMatrix Class Reference</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="dense.html"
                        title="previous chapter">Dense Matrices</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="sparsetools.html"
                        title="next chapter">Sparse Tools</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/matrices/sparse.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="sparsetools.html" title="Sparse Tools"
             >next</a> |</li>
        <li class="right" >
          <a href="dense.html" title="Dense Matrices"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Matrices</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Sparse Matrices</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/matrices/sparse.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:27:05 GMT -->
</html>